﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WinFormStore.Basic
{
    public partial class FrmSaleMan : Form
    {
        public FrmSaleMan()
        {
            InitializeComponent();
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        }

        private void BindCmb()
        {
            this.cmbSex.Items.Add("--请选择--");
            this.cmbSex.Items.Add("男");
            this.cmbSex.Items.Add("女");
            this.cmbSex.Text = "--请选择--";

            this.cmbRole.Items.Add("--请选择--");
            this.cmbRole.Items.Add("店长");
            this.cmbRole.Items.Add("收银员");
            this.cmbRole.Items.Add("导购员");
            this.cmbRole.Text = "--请选择--";
        }
        private void BindData()
        {
            DBHelper.PrepareSql("select * from SalesMan");
            this.dataGridView1.DataSource=DBHelper.ExecuteQuery();
        }
        private void BindNull()
        {
            this.txtname.Text = "";
            this.txtmobile.Text = "";
            this.txtpwd.Text = "";
            this.cmbSex.Text = "--请选择--";
            this.txtWage.Text = "";
            this.txtCommissionRate.Text = "";
            this.cmbRole.Text = "--请选择--";
        }

        private void FrmSaleMan_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns=false;
            BindCmb();
            BindData();
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            string sql = "insert into SalesMan(SalesmanName,Mobile,Pwd,Gender,Wage,CommissionRate,Role) values(@SalesmanName,@Mobile,@Pwd,@Gender,@Wage,@CommissionRate,@Role)";
            DBHelper.PrepareSql(sql);
            DBHelper.SetParameter("SalesmanName",this.txtname.Text);
            DBHelper.SetParameter("Mobile",this.txtmobile.Text);
            DBHelper.SetParameter("Pwd",this.txtpwd.Text);
            DBHelper.SetParameter("Gender",this.cmbSex.Text);
            DBHelper.SetParameter("Wage",this.txtWage.Text);
            DBHelper.SetParameter("CommissionRate",this.txtCommissionRate.Text);
            DBHelper.SetParameter("Role",this.cmbRole.Text);
           
            int rowCount=DBHelper.ExecuteNonQuery();
            if (rowCount == 1)
            {
                MessageBox.Show("添加成功");
                BindData();
            }
            else
            {
                MessageBox.Show("添加失败");
            }
        }
        private void btnEdit_Click(object sender, EventArgs e)
        {
            int SalesmanId = int.Parse(this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
            string sql = "update SalesMan set SalesmanName=@SalesmanName,Mobile=@Mobile,Pwd=@Pwd,Gender=@Gender,Wage=@Wage,CommissionRate=@CommissionRate,Role=@Role where  SalesmanID=@SalesmanID";
            DBHelper.PrepareSql(sql);
            DBHelper.SetParameter("SalesmanName", this.txtname.Text);
            DBHelper.SetParameter("Mobile", this.txtmobile.Text);
            DBHelper.SetParameter("Pwd", this.txtpwd.Text);
            DBHelper.SetParameter("Gender", this.cmbSex.Text);
            DBHelper.SetParameter("Wage", this.txtWage.Text);
            DBHelper.SetParameter("CommissionRate", this.txtCommissionRate.Text);
            DBHelper.SetParameter("Role", this.cmbRole.Text);
            DBHelper.SetParameter("SalesmanID", SalesmanId);
            int rowcount = DBHelper.ExecuteNonQuery();
            if (rowcount == 1)
            {
                MessageBox.Show("数据修改成功！");
                BindData();
                BindNull();
            }
            else
            {
                MessageBox.Show("数据修改失败！");
            }
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确定要删除数据吗?", "删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.Cancel)
            {
                return;
            }
            else
            {
                int SalesmanId = int.Parse(this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                string sql = "delete from SalesMan where SalesmanID=@SalesmanID";
                DBHelper.PrepareSql(sql);
            
                DBHelper.SetParameter("SalesmanID", SalesmanId);
                int rowcount = DBHelper.ExecuteNonQuery();
                if (rowcount == 1)
                {

                    MessageBox.Show("数据删除成功！");
                    BindData();
                    BindNull();
                }
                else
                {
                    MessageBox.Show("数据删除失败！");
                }
            }
        }

        private void dataGridView1_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows[0].Cells[0].Value == null)
            {
                MessageBox.Show("您未选择数据！");
            }
            else
            {
                int SalesmanId = int.Parse(this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                DBHelper.PrepareSql("select * from SalesMan where SalesmanID=@SalesmanID");
                DBHelper.SetParameter("SalesmanID", SalesmanId);
                DataTable dt = new DataTable();
                dt = DBHelper.ExecuteQuery();
                if (dt.Rows.Count != 1)
                {
                    MessageBox.Show("获取不到数据");
                    BindData();
                    return;
                }
                else
                {
                    this.txtname.Text = dt.Rows[0]["SalesmanName"].ToString();
                    this.txtmobile.Text = dt.Rows[0]["Mobile"].ToString();
                    this.txtpwd.Text = dt.Rows[0]["Pwd"].ToString();
                    this.cmbSex.Text = dt.Rows[0]["Gender"].ToString();
                    this.txtWage.Text = dt.Rows[0]["Wage"].ToString();
                    this.txtCommissionRate.Text = dt.Rows[0]["CommissionRate"].ToString();
                    this.cmbRole.Text = dt.Rows[0]["Role"].ToString();
                }
            }
          
        }
    }
}
